/**
 * Created by Eoly on 2017/5/11.
 * Call http://palerock.cn
 */

var GameObject = Main.createConfigClass({
    default: {
        defaultVar: function () {
            return {
                id: Cielar.Utils.buildUniqueId(),
                children: {},
                parent: null
            }
        },
        defaultConfig: function () {
            return {
                name: 'GameObject',
                class: this.constructor
            }
        },
        onConfigured: function () {
            // 使用配置对象来初始化当前对象的属性
            this.name = this.config.name;
            this.class = this.config.class;
            this.num = this.config.num;
        },
        onInitFinish: function () {
            // 生成子节点
            var childrenConfig = this.config.children;
            if (childrenConfig && childrenConfig.push != null) {
                for (var i = 0; i < childrenConfig.length; i++) {
                    this.createChildNode(childrenConfig[i],
                        childrenConfig[i].class ? childrenConfig[i].class : this.constructor);
                }
            }
        }
    },
    defaultPrototype: {
        createChildNode: function (config, func) {
            var child = Cielar.newClass(func, [config], {parent: this});
            if (!(child instanceof this.constructor)) {
                throw new Error('子节点的类型需要与父节点相同');
            }
            this.children[child.id] = child;
            return child;
        },
        showName: function () {
            var name = '';
            var _this = this;
            while (_this && _this.name) {
                name = _this.name + '<-' + name;
                _this = _this.parent;
            }
            console.log(name);
        },
        showAllName: function () {
            this.showName();
            var _this = this;
            _this.children.map(function (key) {
                _this.children[key].showAllName();
            })
        }
    }
});
